from django.http import HttpResponse
from django.shortcuts import render

from .helper_db import get_lunch_items_name_array, get_sorted_orders, calculate_total_from_number, calculate_total_from_dict, add_total_rows_in_list, add_total_rows_in_matrix, update_rows_count, cross_rows
from .helper_pdf import download_pdf

# Create your views here.

mode = 'download-pdfxxxxxxx'
template_many = 'report.items.html'
template_single = 'report.matrix.html'

def test(request):
    return HttpResponse('test')

def index(request):
    return render(request, 'index.html')

def report_items(request):
    order_rows = get_sorted_orders()
    items_name_array = get_lunch_items_name_array()
    context = {
        "items"   : items_name_array,
        "rows"    : update_rows_count(add_total_rows_in_list(order_rows)),
        "qty_dict": calculate_total_from_number(order_rows, items_name_array),
    }
    if mode == 'download-pdf':
        return download_pdf('order_items', template_many, context=context)
    else:
        return render(request, template_many, context=context)

def report_matrix(request):
    order_rows = cross_rows(get_sorted_orders(False))
    items_name_array = get_lunch_items_name_array()
    context = {
        "items"   : items_name_array,
        "rows"    : update_rows_count(add_total_rows_in_matrix(order_rows, items_name_array)),
        "qty_dict": calculate_total_from_dict(order_rows, items_name_array)
    }
    if mode == 'download-pdf':
        return download_pdf('order_matrix', template_single, context=context)
    else:
        return render(request, template_single, context=context)
    



# def report_raw(request):
#     context = {
#         "title" : 'RAW Data Report',
#         "rows"  : get_sorted_orders(False)
#     }
#     return render(request, 'report.raw.html', context=context)

# def pdf_download():
#     return download_pdf('report', 'pdf.download.html', {'myvar': 'this is your template context'})
